package c.c.b.a.a;

import c.b.a.a.a;
import java.io.EOFException;
import java.io.IOException;
import java.util.logging.Logger;
import java.util.zip.CRC32;
import java.util.zip.Inflater;

/* compiled from: GzipSource */
public final class l implements w {
    public int a = 0;
    public final g b;
    public final Inflater c;
    public final m d;
    public final CRC32 e = new CRC32();

    public l(w wVar) {
        if (wVar != null) {
            Inflater inflater = new Inflater(true);
            this.c = inflater;
            Logger logger = o.a;
            r rVar = new r(wVar);
            this.b = rVar;
            this.d = new m(rVar, inflater);
            return;
        }
        throw new IllegalArgumentException("source == null");
    }

    public x a() {
        return this.b.a();
    }

    public long b(e eVar, long j) throws IOException {
        e eVar2 = eVar;
        long j2 = j;
        int i = (j2 > 0 ? 1 : (j2 == 0 ? 0 : -1));
        if (i < 0) {
            throw new IllegalArgumentException(a.B("byteCount < 0: ", j2));
        } else if (i == 0) {
            return 0;
        } else {
            if (this.a == 0) {
                this.b.a(10);
                byte w = this.b.c().w(3);
                Object obj = ((w >> 1) & 1) == 1 ? 1 : null;
                if (obj != null) {
                    k(this.b.c(), 0, 10);
                }
                n("ID1ID2", 8075, this.b.i());
                this.b.p(8);
                if (((w >> 2) & 1) == 1) {
                    long j3;
                    this.b.a(2);
                    if (obj != null) {
                        k(this.b.c(), 0, 2);
                    }
                    long k = (long) this.b.c().k();
                    this.b.a(k);
                    if (obj != null) {
                        j3 = k;
                        k(this.b.c(), 0, k);
                    } else {
                        j3 = k;
                    }
                    this.b.p(j3);
                }
                if (((w >> 3) & 1) == 1) {
                    long f0 = this.b.f0((byte) 0);
                    if (f0 != -1) {
                        if (obj != null) {
                            k(this.b.c(), 0, f0 + 1);
                        }
                        this.b.p(f0 + 1);
                    } else {
                        throw new EOFException();
                    }
                }
                if (((w >> 4) & 1) == 1) {
                    long f02 = this.b.f0((byte) 0);
                    if (f02 != -1) {
                        if (obj != null) {
                            k(this.b.c(), 0, f02 + 1);
                        }
                        this.b.p(f02 + 1);
                    } else {
                        throw new EOFException();
                    }
                }
                if (obj != null) {
                    n("FHCRC", this.b.k(), (short) ((int) this.e.getValue()));
                    this.e.reset();
                }
                this.a = 1;
            }
            if (this.a == 1) {
                long j4 = eVar2.b;
                j2 = this.d.b(eVar2, j2);
                if (j2 != -1) {
                    k(eVar, j4, j2);
                    return j2;
                }
                this.a = 2;
            }
            if (this.a == 2) {
                n("CRC", this.b.l(), (int) this.e.getValue());
                n("ISIZE", this.b.l(), (int) this.c.getBytesWritten());
                this.a = 3;
                if (!this.b.e()) {
                    throw new IOException("gzip finished without exhausting source");
                }
            }
            return -1;
        }
    }

    public void close() throws IOException {
        this.d.close();
    }

    public final void k(e eVar, long j, long j2) {
        s sVar = eVar.a;
        while (true) {
            int i = sVar.c;
            int i2 = sVar.b;
            if (j < ((long) (i - i2))) {
                break;
            }
            j -= (long) (i - i2);
            sVar = sVar.f;
        }
        while (j2 > 0) {
            int i3 = (int) (((long) sVar.b) + j);
            int min = (int) Math.min((long) (sVar.c - i3), j2);
            this.e.update(sVar.a, i3, min);
            j2 -= (long) min;
            sVar = sVar.f;
            j = 0;
        }
    }

    public final void n(String str, int i, int i2) throws IOException {
        if (i2 != i) {
            throw new IOException(String.format("%s: actual 0x%08x != expected 0x%08x", new Object[]{str, Integer.valueOf(i2), Integer.valueOf(i)}));
        }
    }
}
